import json
import random

from mitmproxy import http

# 定义要测试的边界值列表
list_index = [0.00, 0.01, -0.01]


def response(flow: http.HTTPFlow):
    # 先判断要mock的接口url
    if "quote.json" in flow.request.pretty_url and "x=" in flow.request.pretty_url:
        # 把响应内容转换成json格式
        data = json.loads(flow.response.content)
        # 随机生成边界值列表索引
        i = random.randint(0, 2)
        # 从边界值列表中随机取一个值替换成涨幅
        data['data']['items'][0]['quote']['percent'] = list_index[i]
        # 将更改完的内容以字符串的形式加回到flow中
        flow.response.text = json.dumps(data)